import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * The type Add recipe frame.
 *
 * @author Qiyin Huang
 * @date 24/06/10
 */
public class AddRecipeFrame extends JFrame implements ActionListener {
    /**
     * The Confirm button.
     */
    JButton confirmButton = new JButton("confirm");
    /**
     * The Ask steps.
     */
    JLabel askSteps = new JLabel("Please enter the steps for cooking");
    /**
     * The Recipe input.
     */
    JTextArea recipeInput = new JTextArea();
    /**
     * The Button panel.
     */
    JPanel buttonPanel = new JPanel();
    /**
     * The Panel.
     */
    JPanel panel = new JPanel();
    /**
     * The Background label.
     */
    JLabel backgroundLabel = new JLabel();
    /**
     * The constant newRecipe.
     */
    public static Recipe newRecipe;

    /**
     * Instantiates a new Add recipe frame.
     */
    public AddRecipeFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Interface();
                element();
            }
        });
        this.setVisible(true);
    }

    /**
     * Interface.
     */
    public void Interface() {
        this.setTitle("Add recipe page");
        this.pack();
        this.setSize(new Dimension(1200, 675));
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.getContentPane().add(buttonPanel);
        this.setLayout(new BorderLayout());

        Path filePath = Paths.get(System.getProperty("user.dir"), "resources/basicBackground.png");
        String basicBackgroundFilePath = filePath.toString();

        backgroundLabel = new JLabel(new ImageIcon(basicBackgroundFilePath));
        backgroundLabel.setBounds(0, 0, 1200, 675);
        backgroundLabel.setLayout(new BorderLayout());
        backgroundLabel.add(buttonPanel);
        backgroundLabel.setVisible(true);
        panel.add(backgroundLabel);
        this.getContentPane().add(panel);
    }

    /**
     * Element.
     */
    public void element() {
        askSteps.setBounds(300, 50, 800, 50);
        askSteps.setFont(new Font("Posterama", Font.PLAIN, 35));

        recipeInput.setBounds(350, 100, 500, 300);
        recipeInput.setFont(new Font("Posterama", Font.PLAIN, 30));
        recipeInput.setLineWrap(true);
        recipeInput.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(recipeInput);
        scrollPane.setBounds(350, 100, 500, 300);

        confirmButton.setBounds(350, 400, 500, 40);
        confirmButton.setFont(new Font("Posterama", Font.PLAIN, 30));
        confirmButton.setBackground(Color.orange);
        confirmButton.addActionListener(this);

        buttonPanel.setLayout(null);
        buttonPanel.setOpaque(false);
        buttonPanel.add(askSteps);
        buttonPanel.add(scrollPane);
        buttonPanel.add(confirmButton);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();

        if (obj == confirmButton) {
            AddRecipeFrame.newRecipe.setStep(recipeInput.getText());
            String recipeInfo = AddRecipeFrame.newRecipe.printRecipeInfo();

            Object[] options = {"Confirm to add this recipe"};
            UIManager.put("OptionPane.messageFont", new Font("Posterama", Font.PLAIN, 25));
            UIManager.put("OptionPane.buttonFont", new Font("Posterama", Font.PLAIN, 25));
            UIManager.put("OptionPane.minimumSize", new Dimension(600, 400));
            int result = JOptionPane.showOptionDialog(
                    this,
                    recipeInfo,
                    "Recipe Information",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    options,
                    options[0]
            );

            if (result == JOptionPane.YES_OPTION) {
                Main.rc1.addRecipe(newRecipe);
                this.setVisible(false);
                new MenuFrame();
            }
        }
    }
}